﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using Runda.CMS.Common.Entity.vo;
using Runda.CMS.Common.Utility;
using System.Collections.Generic;
using Telerik.WebControls;

namespace Runda.CMS.Portal.ContractFollowUp
{
    public partial class InstrumentUpgradeExtraBatch : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) { 
            
            }
        }
        private bool validateCheck(ref string message)
        {
            bool isValidate = true;
            if (this.employeeGUID.Value == "")
            {
                message = "请选择执行人";
                isValidate = false;
            }
            if (ExpectEndDate.SelectedDate == null)
            {
                message = "请选择期望到达日期";
                isValidate = false;
            }
            return isValidate;
        }
        protected void lnkbtnSaveAndClose_Click(object sender, EventArgs e)
        {
            string message = "";
            if (!validateCheck(ref message))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "saveansrror", "alert('" + message + "');", true);
                return ;
            }
            string comment= "";
            for (int index = 0; index < gridSubInstrument.Items.Count; index++) {
                Label InstrumentID = gridSubInstrument.Items[index].FindControl("InstrumentID") as Label;
                Label InstrumentName = gridSubInstrument.Items[index].FindControl("InstrumentName") as Label;
                Label CustomerName = gridSubInstrument.Items[index].FindControl("CustomerName") as Label;
                Label CustomerID = gridSubInstrument.Items[index].FindControl("CustomerID") as Label;
                Label serialNum = gridSubInstrument.Items[index].FindControl("serialNum") as Label;
                CheckBox isCheck = gridSubInstrument.Items[index].FindControl("isCheck") as CheckBox;
                if (isCheck.Checked)
                {
                    WsmTaskRegisterEntity registerEntity = new WsmTaskRegisterEntity();
                    registerEntity.CreatedBy = CurrentUserID;
                    registerEntity.CreatedOn = DateTime.Now;
                    registerEntity.CustomerID = new Guid(CustomerID.Text);
                    registerEntity.Deleted = false;
                    registerEntity.InstrumentID = new Guid(InstrumentID.Text);
                    registerEntity.InstrumentSerialNum = serialNum.Text.Trim();
                    registerEntity.InstrumentType = new SystemBll().Get<WsmInstrumentInfoEntity>(registerEntity.InstrumentID).CategoryGUID.ToString();
                    registerEntity.PerformPerson = new Guid(employeeGUID.Value);
                    registerEntity.RegisterID = Guid.NewGuid();
                    registerEntity.TaskStatus = 0;
                    registerEntity.TaskType = System.Convert.ToInt32(Enumerator.SalesService.Upgrade);
                    registerEntity.RegisterDate = System.DateTime.Now;
                    if (ExpectEndDate.SelectedDate != null) registerEntity.ExpectEndDate = ExpectEndDate.SelectedDate.Value;

                    new SystemBll().Insert(registerEntity);
                    WsmAccessoryUpgradeRegisterEntity entity;

                    if (gridSubItems.Items.Count > 0) { comment = "升级配件："; }
                    for (int i = 0; i < gridSubItems.Items.Count; i++)
                    {
                        Label AccessoryID = gridSubItems.Items[i].FindControl("AccessoryID") as Label;
                        Label AccessoryName = gridSubItems.Items[i].FindControl("AccessoryName") as Label;
                        comment += AccessoryName.Text.ToString()+"、";
                        entity = new WsmAccessoryUpgradeRegisterEntity();
                        entity.AccessoryID = new Guid(AccessoryID.Text.Trim());
                        entity.RegisterID = registerEntity.RegisterID;
                        entity.UpGradeID = Guid.NewGuid();
                        entity.Deleted = false;
                        new SystemBll().Insert(entity);
                    }
                    if (txtSoftwareVersion.Text.Trim() != "")
                    {
                        comment += " 软件升级版本：" + txtSoftwareVersion.Text.Trim();
                        entity = new WsmAccessoryUpgradeRegisterEntity();
                        entity.RegisterID = registerEntity.RegisterID;
                        entity.UpGradeID = Guid.NewGuid();
                        entity.SoftwareVersion = txtSoftwareVersion.Text.Trim();
                        entity.Deleted = false;
                        new SystemBll().Insert(entity);
                    }
                    comment = comment.Replace("、 ", " ");
                    registerEntity.Comment = comment;
                    new SystemBll().InsertOrUpdate(registerEntity);
                }
            }
            gridSubInstrument.DataSource = getDateFromGridInstrumentForCheck();
            gridSubInstrument.DataBind();
        }

        private DataTable getDateFromGrid(int i)
        {
            DataTable dt = new AccessoryInfo().getAccessoryInfoStructure();
            for (int index = 0; index < gridSubItems.Items.Count; index++)
            {
                Label AccessoryID = gridSubItems.Items[index].FindControl("AccessoryID") as Label;
                Label AccessoryName = gridSubItems.Items[index].FindControl("AccessoryName") as Label;
                if (index != i)
                {
                    DataRow dr = dt.NewRow();
                    dr["AccessoryID"] = AccessoryID.Text;
                    dr["AccessoryName"] = AccessoryName.Text;
                    dt.Rows.Add(dr);
                }
            }
            return dt;
        }
        protected void gridSubItems_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {
                gridSubItems.DataSource = getDateFromGrid(e.Item.ItemIndex);
                gridSubItems.DataBind();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            DataTable dt = getDateFromGrid(-1);
            bool isExist = false;
            for (int index = 0; index < dt.Rows.Count; index++)
            {
                String AccessoryId = dt.Rows[index]["AccessoryID"].ToString();
                if (AccessoryId == hiddenAccessoryID.Value.Trim())
                {
                    isExist = true;
                }
            }
            if (!isExist)
            {
                DataRow dr = dt.NewRow();
                if (hiddenAccessoryID.Value == "") return;
                dr["AccessoryID"] = hiddenAccessoryID.Value.ToString();
                WsmAccessoryInfoEntity entity = new SystemBll().Get<WsmAccessoryInfoEntity>(new Guid(hiddenAccessoryID.Value));
                dr["AccessoryName"] = entity.AccessoryName;
                dt.Rows.Add(dr);
            }
            gridSubItems.DataSource = dt;
            gridSubItems.DataBind();
        }       

        protected void btnAddInstrument_Click(object sender, EventArgs e){
            if (hiddenInstrumentID.Value != "")
            {
                DataTable dt = getDateFromGridInstrument(-1);              
                string instrumentIDStr = hiddenInstrumentID.Value;
                string WhereClause = "";
                WhereClause += " and InstrumentID='" + instrumentIDStr + "' and TaskType="+System.Convert.ToInt32(Enumerator.SalesService.Setup).ToString();
                if (txtSerialNumB.Text.Trim() != "")
                    WhereClause += " and InstrumentSerialNum >= '" + txtSerialNumB.Text.Trim() + "'";
                if (txtSerialNumE.Text.Trim() != "")
                    WhereClause += " and InstrumentSerialNum <= '" + txtSerialNumE.Text.Trim() + "'";

                List<WsmTaskRegisterEntity> registerList = new SystemBll().GetAll<WsmTaskRegisterEntity>(WhereClause); 
                foreach (WsmTaskRegisterEntity register in registerList)
                {  
                    bool isExist = false;
                    for (int index = 0; index < dt.Rows.Count; index++)
                    {
                        String InstrumentID = dt.Rows[index]["InstrumentID"].ToString();
                        String CustomerID = dt.Rows[index]["CustomerID"].ToString();
                        String SerialNum = dt.Rows[index]["SerialNum"].ToString();                                     
                        if (register.InstrumentID.ToString() == instrumentIDStr && register.CustomerID.ToString() == CustomerID && register.InstrumentSerialNum == SerialNum)
                        {
                            isExist = true;
                        }
                    }  
                    if (!isExist)
                    {
                        DataRow dr = dt.NewRow();
                        dr["InstrumentID"] = instrumentIDStr;
                        dr["SerialNum"] = register.InstrumentSerialNum;
                        WsmInstrumentInfoEntity entity = new SystemBll().Get<WsmInstrumentInfoEntity>(new Guid(instrumentIDStr));
                        dr["InstrumentName"] = entity.instrumentName;
                        dr["CustomerID"] = register.CustomerID.ToString();
                        CtrtCustomerEntity userEntity = new SystemBll().Get<CtrtCustomerEntity>(register.CustomerID);
                        dr["CustomerName"] = userEntity.CustomerName;
                        dr["SoftwareVersion"] = new MaintainBillBll().getInstrumentSoftwareversion(register.RegisterID.ToString());
                        dt.Rows.Add(dr);
                    }
                }
             
                gridSubInstrument.DataSource = dt;
                gridSubInstrument.DataBind();
            }
        }
        
        protected void gridSubInstrument_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {
                gridSubInstrument.DataSource = getDateFromGrid(e.Item.ItemIndex);
                gridSubInstrument.DataBind();
            }
        }

        private DataTable getDateFromGridInstrument(int i)
        {
            DataTable dt = new InstrumentInfo().getInstrumentInfoStructure();
            for (int index = 0; index < gridSubInstrument.Items.Count; index++)
            {
                Label InstrumentID = gridSubInstrument.Items[index].FindControl("InstrumentID") as Label;
                Label InstrumentName = gridSubInstrument.Items[index].FindControl("InstrumentName") as Label;
                Label CustomerName = gridSubInstrument.Items[index].FindControl("CustomerName") as Label;
                Label CustomerID = gridSubInstrument.Items[index].FindControl("CustomerID") as Label;
                Label serialNum = gridSubInstrument.Items[index].FindControl("serialNum") as Label;
             
                if (index != i)
                {
                    DataRow dr = dt.NewRow();
                    dr["InstrumentID"] = InstrumentID.Text;
                    dr["InstrumentName"] = InstrumentName.Text;
                    dr["CustomerID"] = CustomerID.Text;
                    dr["CustomerName"] = CustomerName.Text;
                    dr["serialNum"] = serialNum.Text;
                    dt.Rows.Add(dr);
                }
            }
            return dt;
        }

        private DataTable getDateFromGridInstrumentForCheck()
        {
            DataTable dt = new InstrumentInfo().getInstrumentInfoStructure();
            for (int index = 0; index < gridSubInstrument.Items.Count; index++)
            {
                Label InstrumentID = gridSubInstrument.Items[index].FindControl("InstrumentID") as Label;
                Label InstrumentName = gridSubInstrument.Items[index].FindControl("InstrumentName") as Label;
                Label CustomerName = gridSubInstrument.Items[index].FindControl("CustomerName") as Label;
                Label CustomerID = gridSubInstrument.Items[index].FindControl("CustomerID") as Label;
                Label serialNum = gridSubInstrument.Items[index].FindControl("serialNum") as Label;
                CheckBox isCheck = gridSubInstrument.Items[index].FindControl("isCheck") as CheckBox;
                if (!isCheck.Checked)
                {
                    DataRow dr = dt.NewRow();
                    dr["InstrumentID"] = InstrumentID.Text;
                    dr["InstrumentName"] = InstrumentName.Text;
                    dr["CustomerID"] = CustomerID.Text;
                    dr["CustomerName"] = CustomerName.Text;
                    dr["serialNum"] = serialNum.Text;
                    dt.Rows.Add(dr);
                }
            }
            return dt;
        }
    }
}
